package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.wx.search.HistoryKeywordListBean;
import com.cskaoyan.bean.vo.wx.search.SearchIndexVo;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author: zhang
 * @Date: 2022/11/22 22:27
 * @Since: jdk1.8
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public SearchIndexVo index() {
        SearchIndexVo searchIndexVo = new SearchIndexVo();

        Subject subject = SecurityUtils.getSubject();
        //查询默认关键字
        MarketKeywordExample hotExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria hotCriteria = hotExample.createCriteria();
        hotCriteria.andIsDefaultEqualTo(true);
        hotCriteria.andDeletedEqualTo(false);
        hotCriteria.andSortOrderLessThan(Integer.MAX_VALUE);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(hotExample);
        MarketKeyword defaultKeyword = new MarketKeyword();
        Collections.sort(marketKeywords, (MarketKeyword c1, MarketKeyword c2) -> {
            return c2.getSortOrder() - c1.getSortOrder();
        });
        if (marketKeywords.size() != 0) {
            defaultKeyword = marketKeywords.get(0);
        }
        searchIndexVo.setDefaultKeyword(defaultKeyword);
        //查询热门关键字
        MarketKeywordExample defaultExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria defaultCriteria = defaultExample.createCriteria();
        defaultCriteria.andIsHotEqualTo(true);
        defaultCriteria.andDeletedEqualTo(false);
        defaultCriteria.andSortOrderLessThan(Integer.MAX_VALUE);
        List<MarketKeyword> hotKeywordList = marketKeywordMapper.selectByExample(defaultExample);
        Collections.sort(hotKeywordList, (MarketKeyword c1, MarketKeyword c2) -> {
            return c2.getSortOrder() - c1.getSortOrder();
        });
        searchIndexVo.setHotKeywordList(hotKeywordList);
        //登录之后查询历史查询
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser) subject.getPrincipal();
            Integer userId = user.getId();
            MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria historyCriteria = historyExample.createCriteria();
            historyCriteria.andUserIdEqualTo(userId);
            List<HistoryKeywordListBean> historyKeywordList = new ArrayList<>();
            List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(historyExample);
            for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
                String keyword = marketSearchHistory.getKeyword();
                historyKeywordList.add(new HistoryKeywordListBean(keyword));
            }
            searchIndexVo.setHistoryKeywordList(historyKeywordList);
        }
        return searchIndexVo;
    }

    @Override
    public List<String> helper(String keyword) {
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(example);
        List<String> keywords = new ArrayList<>();
        for (MarketKeyword marketKeyword : marketKeywords) {
            keywords.add(marketKeyword.getKeyword());
        }
        return keywords;
    }

    @Override
    public void cleanSearchHistory() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            MarketUser user = (MarketUser) subject.getPrincipal();
            Integer userId = user.getId();
            MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
            marketSearchHistory.setDeleted(true);
            marketSearchHistory.setUpdateTime(LocalDateTime.now());
            marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory, marketSearchHistoryExample);
        }
    }
}
